package plugin;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class PluginRegistryTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSpawnPlugin() throws Exception {
		PluginDescriptor dummyPD = new PluginDescriptor("plugin.DummyPlugin");
		PluginRegistry.instance().addToChain(dummyPD);
		
		PluginDescriptor badPD = new PluginDescriptor("plugin.BadPlugin");
		PluginRegistry.instance().addToChain(badPD);
		
		PluginDescriptor awesomePD = new PluginDescriptor("plugin.AwesomePlugin");
		//this should already be in the chain
		
		//System.err.println(PluginRegistry.instance().getChain());
		
		//in Eclipse classpath
		IProductIDPlugin dummy = PluginRegistry.instance().spawnPlugin(dummyPD);
		String dummyDesc = dummy.identifyProduct("doesn't matter");
		assertEquals(dummyDesc, "I have no idea.");
		
		//XXX: This test is removed from RTM
		//not in Eclipse classpath, but in plugins folder
		//IProductIDPlugin awesome = PluginRegistry.instance().spawnPlugin(awesomePD);
		//String awesomeDesc = awesome.identifyProduct("doesn't matter");
		//assertEquals(awesomeDesc, "I'm awesome!");
		
		IProductIDPlugin bad = PluginRegistry.instance().spawnPlugin(badPD);
		try {
			bad.identifyProduct("doesn't matter");
			fail("Bad plugin didn't throw exception!");
		}
		catch (Exception e) {
			//success!
		}
	}

}
